<?php

namespace App\Admin\Components;

use App\Models\OwnerItem;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Http\Request;

class HouseProductOrders extends Round
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('收入历史表');
        $this->chartLabels(['总收入', '租金总收入', '杂费总收入']);
        $this->dropdown(
            [
                '7' => '最近7天',
                '28' => '最近28天',
                '30' => '最近一个月',
                '365' => '最近一年',
            ]
        );
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '所有':
                $this->getAmount('all');
            case '365':
                $this->getAmount(365);
            case '30':
                $this->getAmount(30);
            case '28':
                $this->getAmount(28);
            case '7':
                $this->getAmount(7);
            default:
                $this->getAmount("all");
        }
    }

    private function getAmount($day)
    {
        if ($day == "all") {
            $amount = doubleval(OwnerItem::query()->whereNotNull('pay_at')->sum('total_amount'));
            if ($amount != 0) {
                $rent_amount = doubleval(OwnerItem::query()->whereNotNull('pay_at')->sum('rent'));
                $other_amount = $amount - $rent_amount;
                $this->withContent($amount, $rent_amount, $other_amount);
                $this->withChart([100, (int)(($rent_amount * 100) / $amount), (int)(($other_amount * 100) / $amount)]);
                $this->chartTotal('合计', $amount);
            } else {
                $this->withContent(0, 0, 0);
                $this->withChart([0, 0, 0]);
                $this->chartTotal('合计', 0);
            }
        } else {
            $amount = doubleval(OwnerItem::query()->where('pay_at', ">", Carbon::now()->subDays($day))->sum('total_amount'));
            if ($amount != 0) {
                $rent_amount = doubleval(
                    OwnerItem::query()->where('pay_at', ">", Carbon::now()->subDays($day))->sum('rent')
                );
                $other_amount = $amount - $rent_amount;
                $this->withContent($amount, $rent_amount, $other_amount);
                $this->withChart([100, (int)$rent_amount / $amount, (int)$other_amount / $amount]);
                $this->chartTotal('合计', $amount);
            } else {
                $this->withContent(0, 0, 0);
                $this->withChart([0, 0, 0]);
                $this->chartTotal('合计', 0);
            }
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public
    function withChart(
        array $data
    ) {
        return $this->chart(
            [
                'series' => $data,
            ]
        );
    }

    /**
     * 卡片内容.
     *
     * @param int $finished
     * @param int $pending
     * @param int $rejected
     *
     * @return $this
     */
    public
    function withContent(
        $finished,
        $pending,
        $rejected
    ) {
        return $this->content(
            <<<HTML
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
    <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-primary"></i>
              <span class="text-bold-600 ml-50">总收入</span>
          </div>
          <div class="product-result">
              <span>{$finished}</span>
          </div>
    </div>

    <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-warning"></i>
              <span class="text-bold-600 ml-50">租金总收入</span>
          </div>
          <div class="product-result">
              <span>{$pending}</span>
          </div>
    </div>

     <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-danger"></i>
              <span class="text-bold-600 ml-50">杂费总收入</span>
          </div>
          <div class="product-result">
              <span>{$rejected}</span>
          </div>
    </div>
</div>
HTML
        );
    }
}
